layui.config({
  base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
  var form = layui.form(),
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    laypage = layui.laypage,
    $ = layui.jquery;

  //加载页面数据
  var newsData = '';
  $.get("/api/score/all", function (data) {
    console.log(data);
    render(eval(data));
  });

  //查询
  $(".search_btn").click(function () {
    var newArray = [];
    var index = layer.msg('查询中，请稍候', {icon: 16, time: 3000, shade: 0.8});
    $.ajax({
      url: "/api/score?Cos_id=" + $("#cos_id").val() + "&Stu_id=" + $("#stu_id").val(),
      type: "GET",
      dataType: "json",
      success: function (data) {
        layer.close(index);
        render(eval(data));
      }
    });
  });

  $(".upload_batch").click(function () {
    var index = layui.layer.open({
      title: "批量上传成绩",
      type: 2,
      content: "upload.html",
      area: ['500px', '300px'],
      success: function (layero, index) {
        setTimeout(function () {
          layui.layer.tips('点击此处返回成绩管理', '.layui-layer-setwin .layui-layer-close', {
            tips: 3
          });
        }, 500)
      }
    });
  });

  //修改选课时间
  $("body").on("click", ".edit", function () {
    var cos_id = $(this).attr("data-cos-id");
    var stu_id = $(this).attr("data-stu-id");
    $.get("/api/score", {Cos_id: cos_id, Stu_id: stu_id}, function (data) {
      //存储对象
      console.log("data length: ", JSON.parse(data).length);
      sessionStorage.setItem("score", data);
      var index = layui.layer.open({
        title: "修改学生成绩",
        type: 2,
        content: "edit.html",
        area: ['500px', '400px'],
        success: function (layero, index) {
          setTimeout(function () {
            layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
              tips: 3
            });
          }, 500)
        }
      });
    });
  });

  $("body").on("click", ".del", function () {  //删除
    var cos_id = $(this).attr("data-cos-id");
    var stu_id = $(this).attr("data-stu-id");
    layer.confirm('确定删除此成绩？', {icon: 3, title: '提示信息'}, function (index) {
      $.ajax({
        url: "/api/score",
        type: "DELETE",
        data: {Cos_id: cos_id, Stu_id: stu_id},
        dataType: "json",
        success: function (data) {
          layer.msg("删除成功！");
          window.location.reload();
        },
        error: function (data) {
          layer.msg("删除失败!");
        }
      });
    });
  });

  function render(that) {
    //渲染数据
    function renderDate(data, curr) {
      var dataHtml = '';
      if (!that) {
        currData = newsData.concat().splice(curr * nums - nums, nums);
      } else {
        currData = that.concat().splice(curr * nums - nums, nums);
      }
      if (currData.length !== 0) {
        for (var i = 0; i < currData.length; i++) {
          dataHtml += '<tr>'
            + '<td align="left">' + currData[i].Cos_id + '</td>'
            + '<td>' + currData[i].Stu_id + '</td>'
            + '<td>' + currData[i].Score + '</td>'
            + '<td>'
            + '<a class="layui-btn layui-btn-mini edit" data-cos-id="' + currData[i].Cos_id + '" data-stu-id="' + currData[i].Stu_id + '"><i class="iconfont icon-edit"></i> 修改</a>'
            + '<a class="layui-btn layui-btn-danger layui-btn-mini del" data-cos-id="' + currData[i].Cos_id + '" data-stu-id="' + currData[i].Stu_id + '"><i class="layui-icon">&#xe640;</i> 删除</a>'
            + '</td>'
            + '</tr>';
        }
      } else {
        dataHtml = '<tr><td colspan="4">暂无数据</td></tr>';
      }
      return dataHtml;
    }

    //分页
    var nums = 10; //每页出现的数据量
    if (that) {
      newsData = that;
    }
    laypage({
      cont: "page",
      pages: Math.ceil(newsData.length / nums),
      jump: function (obj) {
        $(".content").html(renderDate(newsData, obj.curr));
        form.render();
      }
    })
  }
})
